##########
# Crossing Borders: Summary Statistics Immigration and Voting
#########

rm(list = ls())
library(lfe)
library(texreg)
library(here)
library(qs)
library(tidyverse)
library(arsenal)
library(data.table)

cb <- qread(here("data", "cb.qs")); setDT(cb)

source(here("code", "functions.R"))


# Summary stats cb 1999 -----------------------------------------------------------
cb <- cb %>% filter(year != 1987)

cb <- cb %>% 
  mutate(treated = case_when(border15 == 0 ~ "Control",
                             border15 == 1 ~ "Treated",
                             TRUE ~ NA_character_),
         treated = factor(treated, levels = c("Control", "Treated")),
         languagereg2019 = recode(languagereg2019, 
                                  "1" = "German",
                                  "2" = "French",
                                  "3" = "Italian-Romansh",
                                  "4" = "Italian-Romansh"),
         popJanSwiss = popJanSwiss %>% as.numeric,
         dfA = dfA %>% as.numeric,
         popJanForiegn = popJanForiegn %>% as.numeric,
         cbw = cbw %>% as.numeric)

cb1999 <- cb %>% filter(BR == 1, travelMUNmin <= 30, complete.cases(popJanTotal), year == 1999)

cb1999_tab <- tableby(treated ~ frPrct + popJanSwiss + dfA + popJanForiegn + cbw + languagereg2019, weights = popJanTotal,
                   data = cb1999, test = T, digits = 1, numeric.stats = c("meansd", "range")) %>% 
  summary(text = "latex",
          labelTranslations = list(frPrct = "Percent support for anti-immigrant parties",
                                   popJanSwiss = "Swiss population",
                                   dfA = "Total immigrant population",
                                   popJanForiegn = "Resident immigrants",
                                   cbw = "Cross-border workers",
                                   languagereg2019 = "Language Region"),
          pfootnote = T, width = 5)



# Summary stats cb 2015 -----------------------------------------------------------
cb2015 <- cb %>% filter(BR == 1, travelMUNmin <= 30, complete.cases(popJanTotal), year == 2015)

cb2015_tab <- tableby(treated ~ frPrct + popJanSwiss + dfA + popJanForiegn + cbw + languagereg2019, weights = popJanTotal,
                      data = cb2015, test = T, digits = 1, numeric.stats = c("meansd", "range")) %>% 
  summary(text = "latex",
          labelTranslations = list(frPrct = "Percent support for anti-immigrant parties",
                                   popJanSwiss = "Swiss population",
                                   dfA = "Total immigrant population",
                                   popJanForiegn = "Resident immigrants",
                                   cbw = "Cross-border workers",
                                   languagereg2019 = "Language Region"),
          pfootnote = T, width = 5)


# Summary stats ticino 1999 -----------------------------------------------------------
cb1999TI <- cb %>% filter(BR == 1, travelMUNmin <= 30, complete.cases(popJanTotal), year == 1999, canton_name == "TI")

cb1999TI_tab <- tableby(treated ~ frPrct + popJanSwiss + dfA + popJanForiegn + cbw, weights = popJanTotal,
                      data = cb1999TI, test = T, digits = 1, numeric.stats = c("meansd", "range")) %>% 
  summary(text = "latex",
          labelTranslations = list(frPrct = "Percent support for anti-immigrant parties",
                                   popJanSwiss = "Swiss population",
                                   dfA = "Total immigrant population",
                                   popJanForiegn = "Resident immigrants",
                                   cbw = "Cross-border workers"),
          pfootnote = T, width = 5)


# Summary stats ticino 2015 -----------------------------------------------------------
cb2015TI <- cb %>% filter(BR == 1, travelMUNmin <= 30, complete.cases(popJanTotal), year == 2015, canton_name == "TI")

cb2015TI_tab <- tableby(treated ~ frPrct + popJanSwiss + dfA + popJanForiegn + cbw, weights = popJanTotal,
                        data = cb2015TI, test = T, digits = 1, numeric.stats = c("meansd", "range")) %>% 
  summary(text = "latex",
          labelTranslations = list(frPrct = "Percent support for anti-immigrant parties",
                                   popJanSwiss = "Swiss population",
                                   dfA = "Total immigrant population",
                                   popJanForiegn = "Resident immigrants",
                                   cbw = "Cross-border workers"),
          pfootnote = T, width = 5)


# Summary stats sess -----------------------------------------------------------

#This is original data from Beerli et al
sess <- readstata13::read.dta13(here("data", "sess_merged.dta"))
sess$year %>% summary
sess %>% filter(nat4 != "", edu2 == "0all", complete.cases("bfsnr"), complete.cases("BR")) %>% 
  filter(year == 1998) %>% group_by(bin15) %>% 
  summarise(weighted.mean(LNwagerhrl, w = worker, na.rm = T))

## Filter to Swiss workers
sess <- sess %>% filter(nat4 == "ch")

## Define treatments
sess$transBorder15 <- 0
sess$transBorder15[which(sess$bin15 == 1 & sess$year %in% (2000:2003))] <- 1

sess$freeBorder15 <- 0
sess$freeBorder15[which(sess$bin15 == 1 & sess$year > 2003)] <- 1

sess$border15 <- 0
sess$border15[which(sess$bin15 == 1)] <- 1

sess$border30 <- 0
sess$border30[which(sess$bin15 == 2)] <- 1


sess <- sess %>% 
  mutate(treated = case_when(border15 == 0 ~ "Control",
                             border15 == 1 ~ "Treated",
                             TRUE ~ NA_character_),
         treated = factor(treated, levels = c("Control", "Treated")))
sess$year %>% summary


sess1998_tab <- tableby(treated ~ LNwagerhrl + worker, weights = worker, 
                    data = sess %>% filter(BR == 1, distmin <= 30, edu2 == "0all", nat4 == "ch", complete.cases(worker),
                                           year == 1998),
                    test = T, digits = 1, numeric.stats = c("meansd", "range")) %>% 
  summary(text = "latex", pval = T,
          labelTranslations = list(LNwagerhrl = "Log hourly wage",
                                   worker = "Number of workers"),
          pfootnote = T, width = 5)


sess2010_tab <- tableby(treated ~ LNwagerhrl + worker, weights = worker, 
                    data = sess %>% filter(BR == 1, distmin <= 30, edu2 == "0all", nat4 == "ch", complete.cases(worker),
                                           year == 2010),
                    test = T, digits = 1, numeric.stats = c("meansd", "range")) %>% 
  summary(text = "latex", pval = T,
          labelTranslations = list(LNwagerhrl = "Log hourly wage",
                                   worker = "Number of workers"),
          pfootnote = T, width = 5)




# Output main summary stats -----------------------------------------------
outMain <- combine_arsenal_tables(cb1999_tab, cb2015_tab, head1 = "Pre-Reform", head2 = "Post-Reform")
capture.output(cat(outMain), file = here("tables", "summary_stats", "summary_main.tex"))

outTI <- combine_arsenal_tables(cb1999TI_tab, cb2015TI_tab, head1 = "Pre-Reform", head2 = "Post-Reform")
capture.output(cat(outTI), file = here("tables", "summary_stats", "summary_main_TI.tex"))

outSESS <- combine_arsenal_tables(sess1998_tab, sess2010_tab, head1 = "Pre-Reform", head2 = "Post-Reform")
capture.output(cat(outSESS), file = here("tables", "summary_stats", "summary_sess.tex"))





